/**
 * 发帖
 */
var post = {
    editor:null,
    /**
     * 发帖按钮
     */
    postClk:function(){
        var _this = this;
        $("#btn-post").click(function(){
            var params = {
                title    : $("#post-title").val(),
                content  : _this.editor.txt.html(),
                uuid     : getCookie("uuid"),
                userName : getCookie("name"),
                descript : _this.editor.txt.text().length > 300 ? _this.editor.txt.text().substr(0,300) : _this.editor.txt.text(),
                label    : "nodejs",
                textNum  : _this.editor.txt.text().length,
                type     : $("#post-type").attr("data-key"),
                typeName : $("#post-type").text(),
                token    : getCookie("token")
            }
            $.post("/post/post",params,function(res){
                if("0" == res.code){
                    layer.alert("发帖成功!");
                }else{
                    layer.alert(res.message);
                }
            })
        })
    },
    typeChoose:function(){
        $(".input-group-btn").on("click","a",function(){
            var txt = $(this).text();
            var value = $(this).attr("data-key");//保存到数据库的值
            $("#post-type").text(txt).attr("data-key",value);
            $(".input-group-btn li").removeClass("active");
            $(this).parent().addClass("active");
        })
    },
    /**
     * 实例化编辑器
     */
    initEditor:function(){
        var E = window.wangEditor
        var editor = new E('#editor')
        editor.customConfig.uploadImgServer = '/upload';
        editor.customConfig.uploadImgHeaders = {
            'Accept': 'text/x-json'
        }
        editor.customConfig.zIndex = 9;
        editor.customConfig.height = "600px";
        editor.create()
        editor.customConfig.customAlert = function (info) {
            // info 是需要提示的内容
            layer.alert(info)
        }
        this.editor = editor;
    },
    init:function(){
        this.initEditor();
        this.postClk();
        this.typeChoose();
    }
}
 $(function(){
    post.init();
 })